
// 获取api
const api = require("../../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "classShow":false,
    "selectIt":false,
    "orderTitle":""
  },

  // 用户点击微信购买
  wxGo:function(){
    // 检查订单
    this.checkOrder();
  },


   //生成会员制课程模块订单
   menberList(){
    var that=this;
    //生成会员制课程模块订单
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
      var params = {
        "uid": resYtkUserInfo.uid,
        "sessionid":resYtkUserInfo.sessionid,
        "memberSystemid":this.data.loadMemberSystemId,
        "market": getApp().globalData.market
      };
      var url = "/order/buymember";
      api.sendPosts( url,params, function(res) {
        that.setData({
          "buyMemberList":res
      });
      wx.setStorageSync('buymember', res)
      that.getorderContent(res.orderguid)
    })
   },
  //获取订单详细信息
  getorderContent(orderguid) {
    var that = this;
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    var sessionid = resYtkUserInfo.sessionid;
    var uid = resYtkUserInfo.uid;

    var params = {
      "sessionid": sessionid,
      "uid": uid,
      "orderguid": orderguid,
      "market": getApp().globalData.market,
    }
    var url = "/order/getOrderDetail";
    api.sendPosts(url,params, function(res) {
      var yhprice = res.costPrice - res.realprice;
      console.log(res)
      that.setData({
        'courselist': res.courselist[0],
        'lastprice': parseFloat(res.realprice).toFixed(2),
        'price': parseFloat(res.realprice),
        'yhprice': parseFloat(yhprice.toFixed(2))
      })
    })
  },

   //检查订单
   checkOrder(){
    var that=this;
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    var buymember= wx.getStorageSync("buymember");
    var params = {
      "uid": resYtkUserInfo.uid,
      "sessionid":resYtkUserInfo.sessionid,
      "orderguid":buymember.orderguid,
      "orderid": buymember.orderid,
      // "amountdue":this.data.realPrice
      "amountdue":buymember.orderprice
    };
    var url = "/order/checkorder";
    api.sendPosts( url,params, function(res) {
      that.setData({
        "checkOrderList":res
      });
      wx.setStorageSync('checkOrder', res)
      //生成支付订单 用户进行支付
      that.createpayorder();
  })
},

// 生成支付订单 进行支付
createpayorder(){
    var that=this;
    var resYtkUserInfo= wx.getStorageSync("USERDATA");
    var checkOrder= wx.getStorageSync("checkOrder");
    var creatParams = {
      "uid": resYtkUserInfo.uid,
      "sessionid":resYtkUserInfo.sessionid,
      "orderguid":checkOrder.orderguid,
      "orderid": checkOrder.orderid,
      "orderprice":checkOrder.orderprice,
      "market": getApp().globalData.market,
      "gateway": getApp().globalData.gateway
    };
    var url = "/order/createpayorder";
    if(checkOrder.step=="zhifu"){
      api.sendPosts( url,creatParams, function(res) {
        // 测试
        that.setData({
          "creatOrderList":res
        });
        // 生成支付订单
        if(res.errcode == 0){
          var params = {
            "uid": resYtkUserInfo.uid,
            "sessionid":resYtkUserInfo.sessionid,
            "orderid": res.out_trade_no,
            "from": getApp().globalData.from,
            "openid": resYtkUserInfo.openid
          };
          var url = "/wxpay/unifiedOrder";
          api.sendPosts( url,params, function(res) {

            // 测试
            that.setData({
              "unifiedOrderList":res
            });
              //微信统一支付
              if (res.errcode == 0) {
              //调起微信支付
              wx.requestPayment({
                'timeStamp': res.data.timeStamp,
                'nonceStr': res.data.nonceStr,
                'package': res.data.package,
                'signType': res.data.signType,
                'paySign': res.data.paySign,
                'appId': res.data.appId,
                'success': function(res) {
                  wx.showModal({
                    title: '温馨提示',
                    content: '支付成功！',
                    showCancel: false,
                    success: function(res) {
                      cb & cb(true)
                    },
                  })
                },
                'fail': function(res) {
                  cb & cb(false);
                },
                'complete': function(res) {}
              })
            } else {
              wx.showToast({
                title: res.errmsg,
                icon: "none"
              })
            }
        })
        }else{
          wx.showToast({
            title: res.errmsg,
          })
        }
    })
    // 支付成功去签订协议
    }else if(checkOrder.step=="qianyue"){
      wx.showModal({
        title: '温馨提示',
        content: '支付成功！',
        showCancel: false,
        success: function(res) {
          cb & cb(true);
        },
      })
      // 支付成功无需签订协议
    }else if(checkOrder.step=="donothing"){
      wx.showModal({
        title: '温馨提示',
        content: '已购买！',
        showCancel: false,
        success: function(res) {
          cb & cb(true);
        },
      })
    }
  wx.removeStorage({
    key: 'buymember',
    key: 'checkOrder'
  })

},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    // 从订单详情过来
    if(options.orderguid){
      that.setData({
        "orderguid":options.orderguid
      })
    }
    //从普通过来 一般情况
    if(options.loadMemberSystemId){
      that.setData({
        "loadMemberSystemId":options.loadMemberSystemId,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    if(this.data.loadMemberSystemId){
      that.menberList();
    }
    if(this.data.orderguid){
      that.getorderContent(that.data.orderguid)
    }

},



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})